package game;

import java.util.Scanner;

import game.exception.NoEnoughMpException;
import game.hero.Doctor;
import game.hero.Hero;
import game.hero.HeroFactory;
import game.hero.IRecover;
import game.hero.Magic;
import game.hero.Sword;
import game.monster.Monster;
import game.monster.MonsterFactory;
import game.weapon.WeaponFactory;

public class Game {

	private Hero hero;
	private Monster monster;
	private int state = 1;
	Scanner input = new Scanner(System.in);

	public void menu() {
		System.out.println("欢迎来到游戏世界");
		chooseHero();
		WeaponFactory.equipWeapon(hero);
		chooseMonster();
		while (true) {		
		System.out.println("开始第" + state + "局对战");
		int n = chooseMenu();
		switch (n) {
		case 1:
			int num=hero.attack();// 普通攻击
			System.out.println(hero.getName()+"对"+monster.getName()+"造成"+num+"点伤害...");
			monster.setHp(monster.getHp()-num);
			break;
		case 2:
			try {
				skill();// 释放技能
			} catch (NoEnoughMpException e) {
				e.printStackTrace();
			}
			break;
		case 3:
			recoveHP();// 恢复体力
			break;
		default:
			System.out.println("输入错误请重新输入");
		}
		if (calc()>0){//先判断怪兽有没有击败，如果没有击败，怪兽攻击角色
			//怪兽被击败则进入下轮重新创建怪兽
			chooseMonster();
			state++;
			continue;
		}
		int num= monster.attack();
		System.out.println("怪兽对"+hero.getName()+"造成"+num+"点伤害....");
		hero.setHp(hero.getHp()-num);
		if (calc()>0) {
			break;
		}
		}
	}

	private void Skill() {
	}

	/**
	 * 选择职业
	 */
	public void chooseHero() {
		System.out.println("请选择你喜欢的职业：1.剑士2.魔法师3.医生");
		int n = input.nextInt();
		System.out.println("请输入角色名称：");
		String name = input.next();
		hero = HeroFactory.getInstance(n, name);
	}

	public void chooseMonster() {
		int random = (int) (Math.random() * 3);
		monster = MonsterFactory.getInstance(random);
		System.out.println("您的对手是：" + monster.getName());
	}

	public int chooseMenu() {
		System.out.println("1.普通攻击");
		System.out.println("2.使用技能");
		boolean flag = hasRecover();// 判断是否有回复体力功能
		if (flag) {
			System.out.println("3.回复体力");
		}
		int n;
		while (true) {
			System.out.println("请输入：");
			n = input.nextInt();
			if ((n > 0 && n < 3 && !flag) || (n > 0 && n < 4 && flag)) {
				break;
			}
		}
		return n;
	}

	public boolean hasRecover() {// 判断是否具有回复体力功能
		if (hero instanceof IRecover) {
			return true;
		}
		return false;
	}

	/**
	 * 释放技能
	 * 
	 * @throws NoEnoughMpException
	 */
	public void skill() throws NoEnoughMpException {
		int n = 0;
		if (hero instanceof Sword) {
			Sword sword = (Sword) hero;
			n = sword.strick();
			System.out.println("对怪物造成伤害" + n + "伤害");
			monster.setHp(monster.getHp() - n);
		} else if (hero instanceof Magic) {
			Magic magic = (Magic) hero;
			n = magic.fire();
			System.out.println("对怪物造成伤害" + n + "伤害");
		} else if (hero instanceof Doctor) {
			Doctor doctor = (Doctor) hero;
			doctor.dizziness(monster);
			System.out.println("降低怪物" + n + "点攻击力");
		}
	}

	public void recoveHP() {
		if (hasRecover()) {
			try {
				IRecover recover = (IRecover) hero;
				int n = recover.recover();
				System.out.println(hero.getName() + "回复" + n + "点体力");
			} catch (NoEnoughMpException e) {
				e.printStackTrace();
			}
		}
	}
	/**
	 * 计算比赛结果
	 * @return
	 */
	public int calc() {
		if (hero.getHp()<=0) {
			System.out.println(hero.getName()+"体力值为0,游戏结束....");
			return 1;
		}
		if (monster.getHp()<=0) {
			System.out.println(monster.getName()+"被你击败请进入下一关...");
			return 2;
		}
		return 0;
	}
}
